class_name UIGamePauseMenu
extends Control

@onready var animation_player: AnimationPlayer = $AnimationPlayer

@onready var button_group: Control = $ButtonGroup
@onready var resume: UISimpleButtonCanvas = $ButtonGroup/Resume
@onready var options_instruction: UISimpleButtonCanvas = $ButtonGroup/OptionsInstruction
@onready var window_mode_setting: UISimpleButtonCanvas = $ButtonGroup/WindowModeSetting
@onready var return_title_screen: UISimpleButtonCanvas = $ButtonGroup/ReturnTitleScreen
@onready var save_and_exit_game: UISimpleButtonCanvas = $ButtonGroup/SaveAndExitGame

@onready var color_rect: ColorRect = $ColorRect

var level: Level = null

# window mode setting button
@onready var borderless_full_screen: UISimpleButtonCanvas = $ButtonGroupWindowModeSetting/BorderlessFullScreen
@onready var full_screen: UISimpleButtonCanvas = $ButtonGroupWindowModeSetting/FullScreen
@onready var windowed: UISimpleButtonCanvas = $ButtonGroupWindowModeSetting/Windowed
@onready var button_group_window_mode_setting: Control = $ButtonGroupWindowModeSetting


# confirm
enum ConfirmType{
	ReturnTitle, ExitGame
}
@onready var confirm_tip_label: Label = $ButtonGroupConfirm/Label
@onready var confirm_yes_button: UISimpleButtonCanvas = $ButtonGroupConfirm/Yes
@onready var confirm_no_button: UISimpleButtonCanvas = $ButtonGroupConfirm/No
var confirm_type: ConfirmType = ConfirmType.ReturnTitle
@onready var button_group_confirm: Control = $ButtonGroupConfirm

@onready var ui_operation_instruction: UIOperationInstruction = $UIOperationInstruction


func _ready() -> void:
	resume.signal_on_click.connect(on_resume_click)
	options_instruction.signal_on_click.connect(on_options_instruction_click)
	window_mode_setting.signal_on_click.connect(on_window_mode_setting_click)
	return_title_screen.signal_on_click.connect(on_return_title_screen_click)
	save_and_exit_game.signal_on_click.connect(on_save_and_exit_game_click)
	
	borderless_full_screen.signal_on_click.connect(on_borderless_full_screen_click)
	full_screen.signal_on_click.connect(on_full_screen_click)
	windowed.signal_on_click.connect(on_windowed_click)
	
	confirm_yes_button.signal_on_click.connect(on_confirm_yes_click)
	confirm_no_button.signal_on_click.connect(on_confirm_no_click)
	

func on_confirm_yes_click():
	if confirm_type == ConfirmType.ReturnTitle:
		GameManager.save_game()
		animation_player.play("hide_confirm_group")
		await animation_player.animation_finished
		await level.ui_black_fade.fade_in().finished
		LevelManager.transition_2("title_level")
	elif confirm_type == ConfirmType.ExitGame:
		GameManager.save_game()
		animation_player.play("hide_confirm_group")
		await animation_player.animation_finished
		await level.ui_black_fade.fade_in().finished
		get_tree().quit()


func on_confirm_no_click():
	animation_player.play("hide_confirm_group")
	await animation_player.animation_finished
	animation_player.play("show_button_group")	


func on_borderless_full_screen_click():
	GlobalUtils.set_window_mode_fullscreen_borderless()
	

func on_full_screen_click():
	GlobalUtils.set_window_mode_fullscreen()
	

func on_windowed_click():
	GlobalUtils.set_window_mode_windowed()


func on_resume_click():
	level.hide_game_pause_menu()


func on_options_instruction_click():
	animation_player.play("hide_button_group")
	await animation_player.animation_finished
	ui_operation_instruction.animation_player.play("show")
	AudioManager.play_sound("sfx_paper_page_flip")
	

func on_window_mode_setting_click():
	animation_player.play("hide_button_group")
	await animation_player.animation_finished
	animation_player.play("show_window_mode_setting_group")
	

func on_return_title_screen_click():
	confirm_type = ConfirmType.ReturnTitle
	confirm_tip_label.text = "是否确认保存并返回标题界面"
	
	animation_player.play("hide_button_group")
	await animation_player.animation_finished
	animation_player.play("show_confirm_group")
	

func on_save_and_exit_game_click():
	confirm_type = ConfirmType.ExitGame
	confirm_tip_label.text = "是否确认保存并退出游戏"
	
	animation_player.play("hide_button_group")
	await animation_player.animation_finished
	animation_player.play("show_confirm_group")


func show_pause_menu():
	animation_player.play("show_pause_menu")
	

func hide_pause_menu():
	if is_window_mode_setting_open():
		animation_player.play("hide_window_mode_setting_group")
	elif is_confirm_group_open():
		animation_player.play("hide_confirm_group")
	elif is_button_group_open():
		animation_player.play("hide_button_group")
	await animation_player.animation_finished
	animation_player.play("hide_bg")
	await animation_player.animation_finished


func enable_all_button():
	resume.is_enable = true
	options_instruction.is_enable = true
	window_mode_setting.is_enable = true
	return_title_screen.is_enable = true
	save_and_exit_game.is_enable = true
	

func disable_all_button():
	resume.is_enable = false
	options_instruction.is_enable = false
	window_mode_setting.is_enable = false
	return_title_screen.is_enable = false
	save_and_exit_game.is_enable = false
	

func enable_all_window_mode_setting_button():
	borderless_full_screen.is_enable = true
	full_screen.is_enable = true
	windowed.is_enable = true
	

func disable_all_window_mode_setting_button():
	borderless_full_screen.is_enable = false
	full_screen.is_enable = false
	windowed.is_enable = false
	

func enable_all_confirm_button():
	confirm_yes_button.is_enable = true
	confirm_no_button.is_enable = true
	
	
func disable_all_confirm_button():
	confirm_yes_button.is_enable = false
	confirm_no_button.is_enable = false


func is_open():
	return color_rect.modulate.a > 0
	

func is_window_mode_setting_open():
	return button_group_window_mode_setting.modulate.a > 0
	

func is_confirm_group_open():
	return button_group_confirm.modulate.a > 0
	
func is_button_group_open():
	return button_group.modulate.a > 0
	

func is_operation_instruction_open():
	return ui_operation_instruction.nine_patch_rect.modulate.a > 0


func _process(delta: float) -> void:
	if Input.is_action_just_pressed("exit") and not animation_player.is_playing() and not ui_operation_instruction.animation_player.is_playing() and level.ui_black_fade.color_rect.modulate.a <= 0:
		if is_open():
			if is_operation_instruction_open():
				AudioManager.play_sound("sfx_paper_page_flip")
				ui_operation_instruction.animation_player.play("hide")
				await ui_operation_instruction.animation_player.animation_finished
				animation_player.play("show_button_group")
			elif is_window_mode_setting_open():
				AudioManager.play_sound("sfx_menubuttonclick")
				animation_player.play("hide_window_mode_setting_group")
				await animation_player.animation_finished
				animation_player.play("show_button_group")
			elif is_confirm_group_open():
				AudioManager.play_sound("sfx_menubuttonclick")
				animation_player.play("hide_confirm_group")
				await animation_player.animation_finished
				animation_player.play("show_button_group")
			else:
				AudioManager.play_sound("sfx_menubuttonclick")
				level.hide_game_pause_menu()
		else:
			if not level.player or level.player.is_dead or level.current_quick_switch_item_ui: return
			AudioManager.play_sound("sfx_menubuttonclick")
			level.open_game_pause_menu()
